package com.pb.infra.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.auth.entity.custom.PriviPasswdCustom;
import com.pb.infra.auth.entity.pbsys.PriviPasswdPolicy;
import com.pb.infra.auth.entity.pbsys.PriviPasswdPolicyExample;
import com.pb.infra.auth.mapper.custom.MetaPriviPasswdPolicyCustomMapper;
import com.pb.infra.auth.mapper.pbsys.PriviPasswdPolicyMapper;
import com.pb.infra.oth.service.SequenceGeneratorService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.DateTimeUtil;
import com.pb.infra.oth.util.StringUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 法人密码策略
 *
 * @Author:liudebao
 * @Date: 2020/12/23
 */
@Service
public class PriviPasswdPolicyService {

    /**
     * 法人密码策略单表DAO映射
     */
    @Resource
    private PriviPasswdPolicyMapper priviPasswdPolicyMapper;

    /**
     * 法人密码策略多表DAO映射
     */
    @Resource
    private MetaPriviPasswdPolicyCustomMapper metaPriviPasswdPolicyCustomMapper;

    /**
     * 序号自动生成逻辑层
     */
    @Resource
    private SequenceGeneratorService sequenceGeneratorService;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询法人密码策略
     *
     * @param map
     * @return
     */
    public Result select(Map<String, Object> map, Page page, HttpServletRequest request) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        if (!StringUtil.isEmpty(map.get("loginUser")) && Constant.OK.equals(map.get("loginUser").toString())) {
            String token = request.getHeader(jwtTokenConfig.getHeader());
            Claims claims = jwtTokenConfig.getClaimByToken(token);
            String userId = (String)claims.get(jwtTokenConfig.getSub());
            map.put("loginUser", userId);
        }
        List<Map<String, Object>> list = metaPriviPasswdPolicyCustomMapper.selectCorpPolicyData(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    /**
     * 保存法人密码策略
     *
     * @param priviPasswdPolicy 请求参数
     * @return Map<String, Object> 操作返回结果
     */
    @Transactional
    public void insert(PriviPasswdPolicy priviPasswdPolicy, HttpServletRequest request) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        String seqId = sequenceGeneratorService.getNextIdStr("POLICY", "CPP");
        priviPasswdPolicy.setPasswdPolicyId(seqId);
        priviPasswdPolicy.setCreateUser(userId);
        priviPasswdPolicy.setCreateTime(DateTimeUtil.getStrFromNewDate());
        priviPasswdPolicy.setUpdateUser(userId);
        priviPasswdPolicy.setUpdateTime(DateTimeUtil.getStrFromNewDate());
        priviPasswdPolicyMapper.insertSelective(priviPasswdPolicy);
    }

    /**
     * 修改法人密码策略
     *
     * @param priviPasswdPolicy 请求参数
     * @return Map<String, Object> 操作返回结果
     */
    @Transactional
    public void update(PriviPasswdCustom priviPasswdPolicy, HttpServletRequest request) {
        //是否修改密码规则
        if (!StringUtil.isEmpty(priviPasswdPolicy.getCheckFlag())) {
            PriviPasswdPolicyExample priviPasswdPolicyExample = new PriviPasswdPolicyExample();
            PriviPasswdPolicyExample.Criteria criteria = priviPasswdPolicyExample.createCriteria();
            criteria.andUseFlagEqualTo(Constant.EXIST);
            criteria.andCorpIdEqualTo(priviPasswdPolicy.getCorpId());
            PriviPasswdPolicy priviPasswdPolicy1 = new PriviPasswdPolicy();
            priviPasswdPolicy1.setUseFlag(Constant.OK);
            priviPasswdPolicyMapper.updateByExampleSelective(priviPasswdPolicy1, priviPasswdPolicyExample);
            Map<String, Object> map = new HashMap<>();
            map.put("passwdPolicyId", priviPasswdPolicy.getPasswdPolicyId());
            map.put("passwdExpiredTime", DateTimeUtil.getAddTime(priviPasswdPolicy.getActiveDays().intValue()));
            map.put("passwdErrorTimes", priviPasswdPolicy.getMaxRetryTimes());
            map.put("corpId", priviPasswdPolicy.getCorpId());
            metaPriviPasswdPolicyCustomMapper.updateUserPolicy(map);
        }
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        priviPasswdPolicy.setUpdateUser(userId);
        priviPasswdPolicy.setUpdateTime(DateTimeUtil.getStrFromNewDate());
        priviPasswdPolicyMapper.updateByPrimaryKeySelective(priviPasswdPolicy);
    }

    /**
     * 删除密码策略
     *
     * @param map 请求参数
     * @return Map<String, Object> 操作返回结果
     */
    @Transactional
    public Result delete(List<String> map) {
        // 删除参数
        for (String appId : map) {
            List<Map<String , Object>> list = metaPriviPasswdPolicyCustomMapper.selUserPolicy(appId);
            if (list != null && list.size() > 0) {
                return Result.error("-1", "选中的密码策略正在被用户使用不能删除");
            }
            priviPasswdPolicyMapper.deleteByPrimaryKey(appId);
        }
        return Result.OK_EMPTY;
    }

    /**
     *查询所有系统密码策略
     * @return
     */
    public Result selAllPass() {
        return Result.ok(metaPriviPasswdPolicyCustomMapper.selAllPassPolicy());
    }
}
